<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Metrics\Examples;

use App\Admin\Metrics\DatepickerTrait;
use App\Models\GroupOrder;
use App\Models\GroupOrderBatche;
use Dcat\Admin\Widgets\Metrics\Round;
use Illuminate\Http\Request;

class ProductOrders extends Round
{
    use DatepickerTrait;

    /**
     * 处理请求
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        if ($request->has('started')) {
            $res = $this->calculation(
                now()->setDateFrom(
                    $request->get('started')
                )->startOfDay(),
                now()->setDateFrom($request->get('ended'))->endOfDay()
            );
        } else {
            switch ($request->get('option')) {
                case '0':
                    $res = $this->calculation(now()->startOfDay(), now()->endOfDay());

                    break;
                case '365':
                    $res = $this->calculation(now()->startOfYear(), now()->endOfDay());

                    break;
                case '30':
                    $res = $this->calculation(now()->startOfMonth(), now()->endOfDay());

                    break;
                case '90':
                    $res = $this->calculation(now()->startOfQuarter(), now()->endOfDay());

                    break;
                case '7':
                    $res = $this->calculation(now()->startOfWeek(), now()->endOfDay());

                    break;
                case '999':
                    $res = $this->calculation(now()->startOfDecade(), now()->endOfDay());
                    break;
                default:
                    $res = $this->calculation(now()->startOfDay(), now()->endOfDay());
                    break;
            }
        }

        // 卡片内容
        $this->withContent(...$res['content']);

        // 图表数据
        $this->withChart($res['content']);

        // 总数
        $this->chartTotal('总销售额', $res['total']);
    }

    /**
     * 设置图表数据.
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data,
        ]);
    }

    /**
     * 卡片内容.
     *
     * @param int $finished
     * @param int $pending
     * @param int $rejected
     * @param mixed $dfs
     *
     * @return $this
     */
    public function withContent($finished, $rejected, $dfs)
    {
        return $this->content(
            <<<HTML
<div class="col-12 d-flex flex-column flex-wrap text-center" style="max-width: 220px">
    <div class="chart-info d-flex justify-content-between mb-1 mt-2" >
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-primary"></i>
              <span class="text-bold-600 ml-50">总单数</span>
          </div>
          <div class="product-result">
              <span>{$finished}</span>
          </div>
    </div>
         <div class="chart-info d-flex justify-content-between mb-1">
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-success"></i>
              <span class="text-bold-600 ml-50">当前预约数</span>
          </div>
          <div class="product-result">
              <span>{$dfs}</span>
          </div>
    </div>
     <div class="chart-info d-flex justify-content-between mb-1">
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-danger"></i>
              <span class="text-bold-600 ml-50">燃烧KC通证</span>
          </div>
          <div class="product-result">
              <span>{$rejected}</span>
          </div>
    </div>

</div>
HTML
        );
    }

    /**
     * 初始化卡片内容.
     */
    protected function init()
    {
        parent::init();

        $this->title('拼团订单');
        $this->chartLabels(['下单总数','燃烧KC通证', '预约数']);
        $this->dropdown([
            '0' => '今日',
            '7' => '周',
            '30' => '月',
            '90' => '季度',
            '365' => '年',
            '999' => '全部',
        ]);
        $id = $this->id();
        $this->datepicker($id)
            ->click("#{$id} .datepicker .btn-primary")
            ->addVariables([
                'datepicker' => [
                    'start' => date('Y-m-d', strtotime('-7 days')),
                    'end' => date('Y-m-d', time()),
                ],
            ]);
    }

    private function calculation(\Illuminate\Support\Carbon $startOfDay, \Illuminate\Support\Carbon $endOfDay)
    {
        $groupOrder = GroupOrderBatche::query()
            ->whereBetween('end_date', [$startOfDay, $endOfDay])
            ->get();
        $count = $groupOrder->count();
        return [
            'content' => [
                $count,
                $count * 50,
                $groupOrder->where('status',0)->count(),
            ],
            'total' => $count * 500,
        ];
    }
}
